package com.bc.web.controller.admin;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.bc.web.model.Account;
import com.bc.web.model.AccountLog;
import com.bc.web.model.Channel;
import com.bc.web.service.ClearD0Task;
import com.bc.web.service.VirtualAccountService;
import com.fs.common.extend.FSController;
import com.fs.common.util.FSDate;
import com.fs.common.util.FSIDGenerate;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;

public class AccountController extends FSController {

	VirtualAccountService vs = new VirtualAccountService();

	public void search() {
		Page<Account> items = Account.dao.search(super.getSearchCondition());
		List<Channel> channelList = Channel.dao.selectAll();
		List<Account> acList = items.getList();
		for (Account ac : acList) {
			for (Channel channel : channelList) {
				if (ac.getChannelNo().equals(channel.getCode())) {
					ac.put("channelName", channel.getName());
					break;
				}
			}
		}
		setPageResultToRequest("items", items);
		render("basedata/accountList.html");
	}

	public void changeEnable() {
		Account.dao.changeEnable(getPara("id"));
		renderSuccess(false);
	}

	public void addPage() {
		setAttr("channelList", Channel.dao.selectAll());
		render("basedata/accountAdd.html");
	}

	public void add() {
		String accountNo = getPara("merchCode");
		String accountName = getPara("merchName");
		String[] channelCodeArray = getParaValues("channelCode");
		if (channelCodeArray == null || channelCodeArray.length == 0) {
			renderFail("请选择渠道");
		} else {
			for (String channelCode : channelCodeArray) {
				if (Account.dao.select(channelCode, accountNo) == null) {
					Account ac = new Account();
					ac.setId(FSIDGenerate.getUUID());
					ac.setAccountNo(accountNo);
					ac.setAccountName(accountName);
					ac.setChannelNo(channelCode);
					ac.setBalance(0L);
					ac.setD0Balance(0L);
					ac.setEnable(true);
					ac.setCreateDateTime(FSDate.getDateTime());
					ac.save();
				}
				if (Account.dao.select(channelCode, "C00000000000001") == null) {
					// 生成新的渠道收入
					Account incomAc = new Account();
					incomAc.setId(FSIDGenerate.getUUID());
					incomAc.setAccountNo("C00000000000001");
					incomAc.setAccountName("渠道收入");
					incomAc.setChannelNo(channelCode);
					incomAc.setBalance(0L);
					incomAc.setD0Balance(0L);
					incomAc.setEnable(true);
					incomAc.setCreateDateTime(FSDate.getDateTime());
					incomAc.save();
				}
			}
			renderSuccess();
		}
	}

	public void clearD0Amount() {
		ClearD0Task task = new ClearD0Task();
		task.run();
		renderSuccess(false);
	}

	private static int minTimeToInt(String src) {
		src = src.replaceAll(":", "");
		return Integer.parseInt(src);
	}

	public void clearhkAmount() {
		String startTime = "08:59";
		String endTime = "21:25";
		int currentTime = minTimeToInt(new SimpleDateFormat("HH:mm").format(new Date()));

		// 交易时间范围验证
		if (minTimeToInt(startTime) < currentTime && currentTime < minTimeToInt(endTime)) {
			renderHtml("交易时间范围，不能发起虚账操作");
			return;
		}

		List<Account> useAccList = new ArrayList<>();
		String pass = getPara("password");
		if (!pass.equals("abcd" + FSDate.getDate())) {
			renderHtml("调整密码错误");
			return;
		}
		int nerror = 0;
		int nsuccess = 0;
		StringBuffer error = new StringBuffer();
		StringBuffer success = new StringBuffer();
		useAccList = Account.dao.selectHicardAccountNo();
		for (Account account : useAccList) {
			AccountLog acclog = new AccountLog();
			acclog.setId(FSIDGenerate.getUUID());
			acclog.setAccountNo(account.getAccountNo());
			acclog.setPreBanlance(account.getBalance());
			long newBalane = (account.getBalance());// 需要减掉的金额
			acclog.setNextBanlance(newBalane);
			acclog.setCreateDateTime(FSDate.getDateTime());

			if (null != account.getBalance() && account.getBalance() == 0) {
				nsuccess++;
				success.append("虚账号:" + account.getAccountNo() + "清0余额成功\n");
			} else {
				account.setBalance(0L);
				if (account.update()) {
					nsuccess++;
					acclog.save();
					success.append("虚账号:" + account.getAccountNo() + "清0余额成功\n");
				} else {
					nerror++;
					error.append("虚账号:" + account.getAccountNo() + "清0余额失败\n");
				}
			}
		}
		String indexp = nsuccess + "个成功, " + nerror + "个失败  \n";
		if (0 == error.length()) {
			renderHtml(indexp + success.toString());
		} else {
			renderHtml(indexp + error.toString() + "-----\n" + success.toString());
		}
	}

	public void clearmyAmount() {
		String startTime = "08:59";
		String endTime = "21:25";
		int currentTime = minTimeToInt(new SimpleDateFormat("HH:mm").format(new Date()));

		// 交易时间范围验证
		if (minTimeToInt(startTime) < currentTime && currentTime < minTimeToInt(endTime)) {
			renderHtml("交易时间范围，不能发起虚账操作");
			return;
		}

		List<Account> useAccList = new ArrayList<>();
		String pass = getPara("password");
		if (!pass.equals("abcd" + FSDate.getDate())) {
			renderHtml("调整密码错误");
			return;
		}
		int nerror = 0;
		int nsuccess = 0;
		StringBuffer error = new StringBuffer();
		StringBuffer success = new StringBuffer();
		useAccList = Account.dao.selectByhicardEnable();
		for (Account account : useAccList) {
			AccountLog acclog = new AccountLog();
			acclog.setId(FSIDGenerate.getUUID());
			acclog.setAccountNo(account.getAccountNo());
			acclog.setPreBanlance(account.getBalance());
			long newBalane = (account.getBalance());// 需要减掉的金额
			acclog.setNextBanlance(newBalane);
			acclog.setCreateDateTime(FSDate.getDateTime());

			if (null != account.getBalance() && account.getBalance() == 0) {
				nsuccess++;
				success.append("虚账号:" + account.getAccountNo() + "清0余额成功\n");
			} else {
				account.setBalance(0L);
				if (account.update()) {
					nsuccess++;
					acclog.save();
					success.append("虚账号:" + account.getAccountNo() + "清0余额成功\n");
				} else {
					nerror++;
					error.append("虚账号:" + account.getAccountNo() + "清0余额失败\n");
				}
			}
		}
		String indexp = nsuccess + "个成功, " + nerror + "个失败  \n";
		if (0 == error.length()) {
			renderHtml(indexp + success.toString());
		} else {
			renderHtml(indexp + error.toString() + "-----\n" + success.toString());
		}
	}

	public void changeAccount() {
		String amt = getPara("amt");
		String pass = getPara("password");
		String accountNo = getPara("accountNo");
		int aammtt = 0;
		try {
			aammtt = Integer.parseInt(amt);
		} catch (Exception e) {
			renderHtml("Exception : 金额错误:" + e.getMessage());
			return;
		}
		Account changeAccount = Account.dao.select(accountNo);
		if (changeAccount == null || !changeAccount.getEnable()) {
			renderHtml("不存在或者禁用的账号:" + accountNo);
			return;
		}

		if (!pass.equals("abcd" + accountNo + FSDate.getDate())) {
			renderHtml("调整密码错误");
			return;
		}

		AccountLog acclog = new AccountLog();
		acclog.setId(FSIDGenerate.getUUID());
		acclog.setAccountNo(accountNo);
		acclog.setPreBanlance(changeAccount.getBalance());
		long newBalane = (aammtt);// 需要减掉的金额
		acclog.setNextBanlance(newBalane);
		acclog.setCreateDateTime(FSDate.getDateTime());

		if (vs.subMoney(false, changeAccount.getChannelNo(), accountNo, aammtt)) {
			acclog.save();
			renderHtml("修改成功 : " + accountNo + "已经减掉" + aammtt + "分");
		} else {
			renderHtml("修改失败,请稍后重试...");
		}
		return;
	}

}
